<template>
  <div class="mod-config">
    <el-button
    v-if="purview.add==role.add"
      type="text"
      size="mini"
      @click="() => addOrUpdateHandle(undefined, 0)"
    >
      Append
    </el-button>
    <el-tree :data="dataList"  :props="defaultProps" node-key="id">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
          v-if="purview.add==role.add"
            type="text"
            size="mini"
            @click="() => addOrUpdateHandle(undefined, data.id)"
          >
            Append
          </el-button>
          <!--          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>  -->
          <el-button
          v-if="purview.update==role.update"
            type="text"
            size="mini"
            @click="() => addOrUpdateHandle(data.id, undefined)"
          >
            Edit
          </el-button>
          <el-button
          v-if="purview.delete==role.delete"
            type="text"
            size="mini"
            @click="() => deleteHandle(data.id)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./routing-add-or-update";
import rout from "@/api/routing";
export default {
  data() {
    return {
              role:{
         update:this.$role.role_update,
         delete:this.$role.role_delete,
         add:this.$role.role_add
        },
        purview:{},
      defaultProps: {
        children: "children",
        label: "menuName",
      },
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    this.getDataList();
      this.getPurview()
  },
  methods: {
      /**
       * 获取当前页面的权限
       */
      getPurview(){
              rout.purview(this.$route.meta.id).then(res=>{
          this.purview=res.data.data;
      })
      },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;

      rout.findAll().then(({ data }) => {
        if (data) {
          this.dataList = data.data;
        } else {
          this.dataList = [];
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id, ids) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, ids);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        rout.delete(ids).then((res) => {
          if (res.code==200) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
